package it.fuesi.expired.fragments;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import it.fuesi.expired.FormActivity;
import it.fuesi.expired.R;
import it.fuesi.expired.adapters.AbstractAdapter;
import it.fuesi.expired.model.base.IModel;
import it.fuesi.expired.model.base.ModelCollection;
import it.fuesi.expired.touch.SwipeDismissListViewTouchListener;

public abstract class BaseItemFragment<T extends IModel, P extends ModelCollection<T>> extends Fragment implements AdapterView.OnItemClickListener {

    private ListView mList;
    protected AbstractAdapter<T,P> mAdapter;

    private static final String TAG = "BaseItemFragment";

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View rootView = inflater.inflate(R.layout.items_list, container, false);

        mList = (ListView)rootView.findViewById(R.id.items_list);

        mList.setOnItemClickListener(this);

        mAdapter = getAdapter(inflater, inflater.getContext(), this);

        mList.setAdapter(mAdapter);


        setTouchListener();

		
		return rootView;
	}

    private void setTouchListener(){
        SwipeDismissListViewTouchListener touchListener =
                new SwipeDismissListViewTouchListener(
                        mList,
                        new SwipeDismissListViewTouchListener.DismissCallbacks() {
                            @Override
                            public boolean canDismiss(int position) {
                                return true;
                            }

                            @Override
                            public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                                for (int position : reverseSortedPositions) {
                                    mAdapter.delete(position);
                                }

                            }
                        });
        mList.setOnTouchListener(touchListener);
        // Setting this scroll listener is required to ensure that during ListView scrolling,
        // we don't look for swipes.
        mList.setOnScrollListener(touchListener.makeScrollListener());
    }


    protected abstract AbstractAdapter getAdapter(LayoutInflater inflater, Context ctx, Fragment fragment );

    public abstract int getCurrentFragmentId();

    private void updateList(P collection){

        mAdapter.setItemCollection(collection);
    }


    @Override
    public void onStart() {
        super.onStart();
        fetch();
    }

    protected void fetch(){
        mAdapter.fetch();
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.d(TAG, "Clicked Item: " + i);
        final Intent intent = FormActivity.makeIntent(getActivity(), getCurrentFragmentId(),((IModel)adapterView.getAdapter().getItem(i)).get_id() );
        startActivityForResult(intent, FormActivity.REQUEST_CODE);
    }
}
